/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.bigstorage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.bigstorage.entity.Goods;
import com.jeesite.modules.bigstorage.service.GoodsService;

/**
 * 商品表Controller
 * @author zhuqingya
 * @version 2019-12-13
 */
@Controller
@RequestMapping(value = "${adminPath}/bigstorage/goods")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Goods get(String id, boolean isNewRecord) {
		return goodsService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("bigstorage:goods:view")
	@RequestMapping(value = {"list", ""})
	public String list(Goods goods, Model model) {
		model.addAttribute("goods", goods);
		return "modules/bigstorage/goodsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("bigstorage:goods:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Goods> listData(Goods goods, HttpServletRequest request, HttpServletResponse response) {
		goods.setPage(new Page<>(request, response));
		Page<Goods> page = goodsService.findPage(goods);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("bigstorage:goods:view")
	@RequestMapping(value = "form")
	public String form(Goods goods, Model model) {
		model.addAttribute("goods", goods);

		return "modules/bigstorage/goodsForm";
	}

	/**
	 * 保存商品表
	 */
	@RequiresPermissions("bigstorage:goods:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Goods goods) {
		goodsService.save(goods);
		return renderResult(Global.TRUE, text("保存商品表成功！"));
	}
	
	/**
	 * 删除商品表
	 */
	@RequiresPermissions("bigstorage:goods:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Goods goods) {
		goodsService.delete(goods);
		return renderResult(Global.TRUE, text("删除商品表成功！"));
	}
	
}